package com.test.profile.dao;


import com.test.profile.pojo.User;
import com.test.profile.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;


public class UserDaoImpl implements UserDao {
    private QueryRunner runner =new QueryRunner(DBUtil.getDateSource());

    @Override
    public User findByusername(String username) {
       String sql = "select * from tb_user where username = ?";
        try {
            User user  = runner.query(sql, new BeanHandler<User>(User.class), username);
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int  addUser(User user) {

            String sql = "insert into user (username,password) values (?,?)";
          try {
              int count = runner.update(sql, user.getUsername(),user.getPassword());
            return count;
          } catch (SQLException e) {
         e.printStackTrace();
            }
          return 0;
    }

}
